package model;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * 1.新增博客
 * 2.查询博客列表
 * 3.根据ID查询出博客详情
 * 4.删除指定博客
 */
public class BlogDao {
    //新增博客
    public void insert(Blog blog){
        Connection connection=null;
        PreparedStatement preparedStatement=null;
        try {
            //1.建立连接
            connection=DBUtil.getConnection();
            //2.构造sql语句
            String sql="insert into blog values(null,?,?,?,now())";
            preparedStatement=connection.prepareStatement(sql);
            preparedStatement.setString(1,blog.getTitle());
            preparedStatement.setString(2,blog.getContent());
            preparedStatement.setInt(3,blog.getUserId());
            //3.提交
            int ret=preparedStatement.executeUpdate();
            if (ret==1){
                System.out.println("提交成功");
            }else{
                System.out.println("提交失败");
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            DBUtil.close(connection,preparedStatement,null);
        }
    }
    //获取所有博客
    public List<Blog> queryBlogAll(){
        Connection connection=null;
        PreparedStatement preparedStatement=null;
        ResultSet resultSet=null;
        List<Blog> blogList=new ArrayList<>();
        try {
            connection = DBUtil.getConnection();
            String sql="select * from blog order by postTime desc";
            preparedStatement=connection.prepareStatement(sql);
            resultSet=preparedStatement.executeQuery();
            while (resultSet.next()){
                Blog blog=new Blog();
                blog.setBlogId(resultSet.getInt("blogId"));
                blog.setTitle(resultSet.getString("title"));
                String content =resultSet.getString("content");
                if (content.length()>=100){
                    content=content.substring(0,100);
                }
                blog.setContent(content);
                blog.setUserId(resultSet.getInt("userId"));
                blog.setPostTime(resultSet.getTimestamp("postTime"));
                blogList.add(blog);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            DBUtil.close(connection,preparedStatement,resultSet);
        }
        return blogList;
    }
    //通过id查询博客
    public Blog queryBlogById(int blogId){
        Connection connection=null;
        PreparedStatement preparedStatement=null;
        ResultSet resultSet=null;
        try {
            connection=DBUtil.getConnection();
            String sql="select * from blog where blogId = ?";
            preparedStatement=connection.prepareStatement(sql);
            preparedStatement.setInt(1,blogId);
            resultSet=preparedStatement.executeQuery();
            if (resultSet.next()){
                Blog blog=new Blog();
                blog.setBlogId(resultSet.getInt("blogId"));
                blog.setTitle(resultSet.getString("title"));
                blog.setContent(resultSet.getString("content"));
                blog.setUserId(resultSet.getInt("userId"));
                blog.setPostTime(resultSet.getTimestamp("postTime"));
                return blog;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            DBUtil.close(connection,preparedStatement,resultSet);
        }
        return null;
    }
    public void deleteBlogById(int blogId){
        Connection connection=null;
        PreparedStatement preparedStatement=null;
        try {
            connection=DBUtil.getConnection();
            String sql="delete from blog where blogId= ? ";
            preparedStatement=connection.prepareStatement(sql);
            preparedStatement.setInt(1,blogId);
            int ret=preparedStatement.executeUpdate();
            if (ret==1){
                System.out.println("删除成功");
            }else{
                System.out.println("删除失败");
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            DBUtil.close(connection,preparedStatement,null);
        }
    }
}
